from qiniu import Auth
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from django.http import JsonResponse


@csrf_exempt  # 允许跨域访问
@api_view(['GET'])
def get_token(request):
    '''
    用来获取upload 所需 token
    '''
    # 1. 先要设置AccessKey和SecretKey
    access_key = "access_key"
    secret_key = "access_key"
    # 2. 授权
    q = Auth(access_key, secret_key)
    # 3. 设置七牛空间
    bucket_name = 'test'
    # 4. 生成token
    token = q.upload_token(bucket_name)
    # 5. 返回token,key必须为uptoken
    return JsonResponse({'uptoken': token})
